function [pdf,logpdf]=normtrpdf(x,mu,sigma,lim) 
%function [pdf,logpdf]=normtrpdf(x,mu,sigma,lim); 
%Computes pdf and log pdf of 
%         a univariate truncated normal x~N(mu,sigma) 
%   lim=[a,b]  s.t.   a <= x <= b
%   sigma is the variance
% See Greene Ch 24.2 page 864 
a=lim(1); 
b=lim(2); 
sd=sqrt(sigma); 
if x < a || x > b 
    disp('Warning , x is outside bounds you provided') 
    pdf=0; 
    logpdf = log( 1e-50 );
    return
end 
if b <= a; 
    error(' Invalid entry a/b in pdfnormt'); 
end; 
pdf=((1/sigma)*normpdf((x-mu)/sd,0,1))/( normcdf((b-mu)/sd,0,1)-normcdf((a-mu)/sd,0,1)); 
logpdf=log(pdf); 